/**
 * 作者: wgx
 * 日期: 2025-03-12 11:04:48
 * 备注: 物资争夺全服悬赏信息
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.CsGameFight.PBServerSnatchOffer;
import com.motu.monstercity.protocol.CsGameSystem.CSGameSystemPushDataResponse;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.base.CommonUtils;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.bag.ItemId;
import com.motu.monstercity.server.game.commondata.mail.MailConstant;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.logic.*;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.bouncycastle.pqc.crypto.newhope.NHOtherInfoGenerator.PartyU;

import java.util.ArrayList;
import java.util.List;

public class ServerSnatchOffer extends ServerSnatchOfferModel {
    public ServerSnatchOffer() {
        super();
    }

    public ServerSnatchOffer(UserInfo userInfo, UserInfo userInfoTarget, int type) {
        super();
        putServerId(userInfo.getServerId());
        putUserId(userInfoTarget.getId());
        putUserName(userInfoTarget.getUserName());
        putPower(GameUser.countUserPower(userInfoTarget.getId(), 0));
        putPartner(PartnerManager.createPartnerPhoto(userInfoTarget.getId(), null));
        putIcon(userInfoTarget.getIcon());
        putType(type);
        putUserIdPublish(userInfo.getId());
        long curTime = TimeUtils.getCurTime();
        putPublishTime(curTime);
        putCompleteTime(curTime + AllParam.SNATCH_OFFER_DURATION_TIME);
    }

    // 是否完成
    public boolean isComplete() {
        if (getUserIdPublish() > 0) {
            if (TimeUtils.getTimeDiff(getPublishTime()) > AllParam.SNATCH_OFFER_DURATION_TIME) {
                return true;// 超过12小时，完成
            }
            JsonArray jsonArray = getFighterArray();
            int max = Tool.isInList(jsonArray, getUserIdPublish()) ? AllParam.SNATCH_OFFER_FIGHT_USER_MAX + 1 : AllParam.SNATCH_OFFER_FIGHT_USER_MAX;
            if (getFighterArray().size() >= max) {// 发布悬赏的人不算在10名的上限内
                return true;// 追击人数超过10人，完成
            }
        }
        return false;
    }

    // 是否高级悬赏
    public boolean isOfferPro() {
        if (getType() == 2) {
            return true;
        } else {
            return false;
        }
    }

    // 悬赏对象的干员快照，战力列表
    public List<Long> getPowerList() {
        List<Long> powerTargetList = new ArrayList<>();// 对方的战力列表
        JsonArray partnerPhoto = new JsonArray(getPartner());// 对方的干员快照
        for (int i=0; i<partnerPhoto.size(); i++) {
            JsonObject jsonObject = partnerPhoto.getJsonObject(i);
            powerTargetList.add(jsonObject.getLong("totalPower"));
        }
        return powerTargetList;
    }

    // 给发布悬赏的玩家，发放悬赏完成的奖励,
    public void sendCompleteReward() {
        long userIdPublish = getUserIdPublish();// 发布悬赏的玩家id
        int multiple = isOfferPro() ? AllParam.SNATCH_FIGHT_PRO_MUL : 1;// 高级悬赏5倍

        // 贸易币，扩建工具，通过邮件发放
        List<RewardItem> rewardItemList = new ArrayList<>();// 奖励
        RewardItem rewardItem = new RewardItem(ItemId.FIGHT_SNATCH_TRANSFER_ITEM, FightManager.takeTransferItemNum(getKoNum()) * multiple);
        rewardItemList.add(rewardItem);
        RewardItem rewardItem2 = new RewardItem(ItemId.FIGHT_SNATCH_MONEY, FightManager.takeFightSnatchMoneyNum(getKoNum()) * multiple);
        rewardItemList.add(rewardItem2);
        MailManager.sendSystemMail(userIdPublish, MailConstant.SNATCH_OFFER_REWARD_TITLE, MailConstant.SNATCH_OFFER_REWARD_CONTENT, rewardItemList, LogType.FIGHT_SNATCH_OFFER_COMPLETE);

        // 物资数量需要推送给玩家
        UserInfo userInfo = GameUser.getUserInfo(userIdPublish);
        CSGameSystemPushDataResponse.Builder response = CSGameSystemPushDataResponse.newBuilder();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        UserSnatchScore userSnatchScore = FightManager.getUserSnatchScore(userIdPublish);
        userSnatchScore.addScore(pbUserData, userInfo, null, FightManager.getAddScore(getKoNum()) * multiple, LogType.FIGHT_SNATCH_OFFER_COMPLETE);
        userSnatchScore.update();
        GameUser.pushPBUserData(userIdPublish, response);//推送PBUserData给客户端进行更新
        putIsSendReward(1);// 标记已发放奖励
    }

    public PBServerSnatchOffer.Builder takeInitBuilder() {
        PBServerSnatchOffer.Builder sb = super.takeInitBuilder();
        UserInfo userInfoPublish = GameUser.getUserInfo(userIdPublish);
        sb.setUserNamePublish(userInfoPublish.getUserName());

        UserInfo userInfo = GameUser.getUserInfo(getUserId());
        if (userInfo != null) {
            sb.setVip(userInfo.getVip());
            sb.setPartnerNum(PartnerManager.countPartnerNum(getUserId()));
            sb.setLevel(userInfo.getLevel());
        }

        return sb;
    }
}
